package route

import (
	"fmt"

	"gitee.com/zhangycsz/glin"
	"gitee.com/zhangycsz/glin/example/controller/app"
	"gitee.com/zhangycsz/glin/example/controller/app/menu"
	"gitee.com/zhangycsz/glin/middleware"
)

func Start() {
	fmt.Println("start glin")
	g := glin.New()
	g.Use(middleware.Logger())

	g.Use(func(next glin.HandlerFunc) glin.HandlerFunc {
		return func(c glin.Context) error {
			return next(c)
		}
	})
	g.GET("/index", func(c glin.Context) error {
		return c.String(200, c.Path())
	})
	g.GET("/list/*", func(c glin.Context) error {
		return c.String(200, c.Path())
	})
	g.GET("/add/:id", func(c glin.Context) error {
		return c.String(200, c.Path())
	})

	group := g.Group("/zhangyc", func(next glin.HandlerFunc) glin.HandlerFunc {
		return func(c glin.Context) error {
			fmt.Println("admin-----")
			return next(c)
		}
	})

	glin.AutoRegisterGroup(group, &app.Index{}, "")
	glin.AutoRegisterGroup(group, &app.Account{}, "")
	glin.AutoRegisterGroup(group, &menu.Menu{}, "")

	for _, v := range g.Routes() {
		fmt.Println("v Method=", v.Method, "path=", v.Path, "name=", v.Name)
	}
	err := g.Start("0.0.0.0:8086")
	if err != nil {
		fmt.Println("err=", err)
	} else {
		fmt.Println("ok")
	}
}
